package com.well.WellProject.Tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ResultUtil {

	// 传出值的字段定义
	public static final String field_outData = "Data";
	public static final String field_outStatus = "ResultData";
	public static final String field_outResult = "Result";
	public static final String field_outMsg = "ErrMsg";

	// 调用数据库的字段定义
	public static final String field_dbResult = "Result";
	public static final String field_dbMsg = "Msg";

	/*----------------------------------------分隔线 传出数据部分------------------------------------*/

	/**
	 * 返回 服务器接口异常,Result:1
	 */
	public static String outExceptString() {
		return outString(1, "服务器异常");
	}

	/**
	 * 返回 Result:1
	 */
	public static String outErrorString() {
		return outString(1);
	}

	/**
	 * 返回 result:0
	 */
	public static String outString() {
		return outString(0);
	}

	/**
	 * 返回 result:x
	 */
	public static String outString(int result) {
		return outString(result, null, null);
	}

	/**
	 * 返回 result:0,data:x
	 */
	public static String outString(boolean result) {
		return outString(0, null, result);
	}

	/**
	 * 返回 result:0,data:x
	 */
	public static String outString(JSONObject json) {
		return outString(0, null, json);
	}

	/**
	 * 返回 result:0,data:x
	 */
	public static String outString(JSONArray jsonArray) {
		return outString(0, null, jsonArray);
	}

	/**
	 * 返回 result:x,msg:x
	 */
	public static String outString(int result, String msg) {
		return outString(result, msg, null);
	}

	/**
	 * 解析map 返回 result:x,msg:x
	 */
	public static String outString(Map<String, Object> paramMap) {
		int result = getResult(paramMap);
		String msg = getMsg(paramMap);
		return outString(result, msg, null);
	}

	/**
	 * 解析map 返回指定参数 result:x,msg:x,data:{key:value}
	 */
	public static String outString(Map<String, Object> paramMap, String[] keys) {
		int result = getResult(paramMap);
		String msg = getMsg(paramMap);
		if (keys == null) {
			return outString(result, msg, null);
		}
		JSONObject dataJson = getDataJson(paramMap, keys);
		return outString(result, msg, dataJson);
	}

	/**
	 * 解析map 返回 result:x,msg:x,data:x
	 */
	public static String outString(Map<String, Object> paramMap, Object data) {
		int result = getResult(paramMap);
		String msg = getMsg(paramMap);
		return outString(result, msg, data);
	}

	/**
	 * 解析map 返回指定参数 result:x,msg:x,data:{key:value}
	 */
	public static String outString2(Map<String, Object> paramMap, String[] keys) {
		int result = getResult(paramMap);
		String msg = getMsg(paramMap);
		if (keys == null) {
			return outString(result, msg, null);
		}
		return outString(result, msg, keys);
	}
	
	/**
	 * 返回 result:x,msg:x,data:x
	 */
	public static String outString(int result, String msg, Object data) {
		JSONObject responseState = new JSONObject();
		responseState.put(field_outResult, result);
		responseState.put(field_outMsg, msg);
		JSONObject responseMain = new JSONObject();
		responseMain.put(field_outStatus, responseState);
		responseMain.put(field_outData, data);
		return responseMain.toJSONString();
	}
	

	/*----------------------------------------分隔线 处理数据部分------------------------------------*/

	/**
	 * 将输入的jsonString转换为参数
	 * 
	 * @param data
	 * @return
	 */
	public static Map<String, Object> getParamMap(String data) {
		JSONObject dataJson = JSON.parseObject(data);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		for (Entry<String, Object> entry : dataJson.entrySet()) {
			String key = entry.getKey();
			Object object = entry.getValue();
			if ("LastUpdateTime".equals(key)) {
//				object = ConvertUtil.convertToDate(object, "yyyy-MM-dd HH:mm:ss:SSS");
			}
			paramMap.put(key, object);
		}
		return paramMap;
	}

	/**
	 * 将指定参数转换为json
	 * 
	 * @param paramMap
	 * @return
	 */
	static JSONObject getDataJson(Map<String, Object> paramMap, String[] keys) {
		JSONObject dataJson = new JSONObject();
		for (int i = 0; i < keys.length; i++) {
			String key = keys[i];
			if (key.toLowerCase().contains("time")) {
				dataJson.put(key, paramMap.get(key).toString());
			} else {
				dataJson.put(key, paramMap.get(key));
			}
		}
		return dataJson;
	}

	/**
	 * 将指定参数转换为json
	 * 
	 * @param paramMap
	 * @return
	 */
	static JSONObject getDataJson(Map<String, Object> paramMap) {
		JSONObject dataJson = new JSONObject();
		for (Entry<String, Object> entry : paramMap.entrySet()) {
			String key = entry.getKey();
			if (key.toLowerCase().contains("time")) {
				dataJson.put(key, entry.getValue().toString());
			} else {
				dataJson.put(key, entry.getValue());
			}
		}
		return dataJson;
	}

	/**
	 * 设置Data数据值
	 */
	public static String setData(String resString, String key, Object value) {
		JSONObject json = JSONObject.parseObject(resString);
		JSONObject jsonData = json.getJSONObject(field_outData);
		jsonData.put(key, value);
		return json.toJSONString();
	}

	/**
	 * 获取result的数值
	 * 
	 * @param paramMap
	 * @return
	 */
	public static int getResult(Map<String, Object> paramMap) {
		Object resultObj = paramMap.get(field_dbResult);
		if (resultObj == null) {
			return 0;
		}
		return Integer.parseInt(resultObj.toString());
	}

	/**
	 * 获取result的数值
	 * 
	 * @param paramMap
	 * @return
	 */
	public static int getResult(String responseString) {
		JSONObject json = JSONObject.parseObject(responseString);
		JSONObject jsonStatus = json.getJSONObject(field_outStatus);
		Object resultObj = jsonStatus.get(field_outResult);
		if (resultObj == null) {
			return 0;
		}
		return Integer.parseInt(resultObj.toString());
	}

	/**
	 * 获取msg
	 * 
	 * @param paramMap
	 * @return
	 */
	public static String getMsg(Map<String, Object> paramMap) {
		Object msgObj = paramMap.get(field_dbMsg);
		if (msgObj == null) {
			return null;
		}
		return msgObj.toString();
	}

	/**
	 * 获取msg
	 * 
	 * @param paramMap
	 * @return
	 */
	public static String getMsg(String responseString) {
		JSONObject json = JSONObject.parseObject(responseString);
		JSONObject jsonStatus = json.getJSONObject(field_outStatus);
		Object msgObj = jsonStatus.get(field_outMsg);
		if (msgObj == null) {
			return null;
		}
		return msgObj.toString();
	}

	/**
	 * 获取返回字符串中的Data,并转换为JSONObject
	 * 
	 * @param responseString
	 * @param key
	 * @return
	 */
	public static JSONObject getData(String responseString) {
		JSONObject json = JSONObject.parseObject(responseString);
		return json.getJSONObject(field_outData);
	}

	/**
	 * 获取返回字符串中的Data.Key,并转换为JSONObject
	 * 
	 * @param responseString
	 * @param key
	 * @return
	 */
	public static Object getData(String responseString, String key) {
		JSONObject json = JSONObject.parseObject(responseString);
		JSONObject jsonData = json.getJSONObject(field_outData);
		return jsonData.get(key);
	}

	/**
	 * 获取返回字符串中的Data,并转换为JSONArray
	 * 
	 * @param responseString
	 * @param key
	 * @return
	 */
	public static JSONArray getDataArray(String responseString) {
		JSONObject json = JSONObject.parseObject(responseString);
		return json.getJSONArray(field_outData);
	}

	/**
	 * 获取返回字符串中的Data.Key,并转换为JSONArray
	 * 
	 * @param responseString
	 * @param key
	 * @return
	 */
	public static JSONArray getDataArray(String responseString, String key) {
		JSONObject json = JSONObject.parseObject(responseString);
		json = json.getJSONObject(field_outData);
		return json.getJSONArray(key);
	}

	/**
	 * 判断数据库的返回结果是否异常
	 * 
	 * @param paramMap
	 * @return
	 */
	public static boolean isResultError(Map<String, Object> paramMap) {
		return getResult(paramMap) != 0;
	}

	/**
	 * 判断接口的返回结果是否异常
	 * 
	 * @param paramMap
	 * @return
	 */
	public static boolean isResultError(String responseString) {
		return getResult(responseString) != 0;
	}

	/**
	 * 通过传入jsonarray的值，对其解析成List<Map<String, Object>>
	 * 
	 * @param arrayStr
	 * @return
	 */
	public static List<Map<String, Object>> getParamArray(String arrayStr) {
		JSONArray jarray = JSONArray.parseArray(arrayStr);
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		Map<String, Object> paraMap = null;
		for (Object obj : jarray) {
			paraMap = ResultUtil.getParamMap(obj.toString());
			resultList.add(paraMap);
		}
		return resultList;

	}
	
	/**
	 * 
	* @Title: getErrMsgJson 
	* @Description: TODO(描述) 
	* @return JSONObject    
	* @throws 
	* @date 2016年12月29日 下午4:56:13
	 */
	public static JSONObject getErrMsgJson(String content) {
		JSONObject jsonErr=new JSONObject();
		jsonErr.put("ErrMsg", content);
		jsonErr.put("Result", -10);
		return jsonErr;
	}
}
